﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class JournalVoucher : RibbonForm
    {
        public JournalVoucher()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }

        public DataTable LineItems { get; set; }

        public void ShowSearch()
        {
            FND_JOURNALVOUCHER oForm = new FND_JOURNALVOUCHER();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            JVManager oManager = new JVManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dt = oManager.GetJournalVoucher(oForm.SelectedDocumentNumber);

            oManager.Close();

            DataRow oRow = dt.Rows[0];

            txtDocNum.Text = oRow["DocNum"].ToString();
            txtCardCode.Text = oRow["CardCode"].ToString();
            txtCardName.Text = oRow["CardName"].ToString();
            txtRemarks.Text = oRow["Remarks"].ToString();
            txtStatus.Text = oRow["DocStatus"].ToString();
            dtPostingDate.EditValue = (DateTime)oRow["DocDate"];
            dtDateCreated.EditValue = (DateTime)oRow["DateCreated"];
            txtCreatedBy.Text = oRow["CreatedBy"].ToString();
            dtDateModified.EditValue = (DateTime)oRow["DateModified"];
            txtModifiedBy.Text = oRow["ModifiedBy"].ToString();

            dt = new DataTable();

            dt = oManager.GetJournalVoucherLineItems(oForm.SelectedDocumentNumber);
            this.LineItems = dt;
            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

            btnAdd.Text = "Update";

            if (gridView1.RowCount == 0)
            {
                if (txtStatus.Text == "Draft")
                {
                    gridView1.AddNewRow();
                }
            }
        }

        void PostDocument()
        {
            JVManager oManager = new JVManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

                oManager.PostDocument(txtDocNum.Text);

            oManager.Close();

            txtStatus.Text = "Posted";
        }


        void CancelDocument()
        {
            JVManager oManager = new JVManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CancelDocument(txtDocNum.Text);

            oManager.Close();

            txtStatus.Text = "Canceled";
        }


        void CloseDocument()
        {
            JVManager oManager = new JVManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CloseDocument(txtDocNum.Text);

            oManager.Close();

            txtStatus.Text = "Closed";
        }

        void InitializeSeries()
        {

            string ObjectType = "14";
            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();

            if (oSeries.IsAutoNumber(ObjectType) == true)
            {
                txtDocNum.Properties.ReadOnly = true;
                txtDocNum.Text = "##########";

            }
            oSeries.Close();



        }

        string GetNewSeries()
        {
            string _SERIES = "";
            string _OBJECT_TYPE = "14";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_OBJECT_TYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_OBJECT_TYPE);
            }
            else
            {
                _SERIES = txtDocNum.Text;
            }
            oSeries.Close();

            return _SERIES;
        }

        void DisableControls()
        {
            txtDocNum.Enabled = false;
            txtCardCode.Enabled = false;
            txtCardName.Enabled = false;
            txtRemarks.Enabled = false;

            dtDateCreated.Enabled = false;
            txtCreatedBy.Enabled = false;
            dtDateModified.Enabled = false;
            txtModifiedBy.Enabled = false;

            btnAdd.Enabled = false;

            foreach (DevExpress.XtraGrid.Columns.GridColumn oColumn in gridView1.Columns)
            {
                oColumn.OptionsColumn.AllowEdit = false;
            }

        }

        void EnableControls()
        {
            txtDocNum.Enabled = true;
            txtCardCode.Enabled = true;
            txtCardName.Enabled = true;
            txtRemarks.Enabled = true;

            dtDateCreated.Enabled = true;
            txtCreatedBy.Enabled = true;
            dtDateModified.Enabled = true;
            txtModifiedBy.Enabled = true;

            btnAdd.Enabled = true;

            foreach (DevExpress.XtraGrid.Columns.GridColumn oColumn in gridView1.Columns)
            {
                oColumn.OptionsColumn.AllowEdit = true;
            }
        }

        DataTable GetTable()
        {
            DataTable dt = new DataTable();


            dt.Columns.Add("Particulars", typeof(string));
            dt.Columns.Add("AcctCode", typeof(string));
            dt.Columns.Add("AcctName", typeof(string));
            dt.Columns.Add("Debit", typeof(double));
            dt.Columns.Add("Credit", typeof(double));

            return dt;
        }

        void AddJournalVoucher()
        {

        }


        private void txtAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;

            oForm.ShowDialog();

            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctCode"], oForm.SelectedGLAcctCode);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctName"], oForm.SelectedGLAcctName);

        }

        private void JournalVoucher_Load(object sender, EventArgs e)
        {
            InitializeSeries();
            this.LineItems = GetTable();

            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

            if (gridView1.RowCount <= 0)
            {
                gridView1.AddNewRow();
            }
        }

        private void JournalVoucher_Shown(object sender, EventArgs e)
        {
            dtDateCreated.EditValue = System.DateTime.Now;
            txtCreatedBy.Text = this.ActiveUserID;
            dtDateModified.EditValue = System.DateTime.Now;
            txtModifiedBy.Text = this.ActiveUserID;

            


        }

        private void JournalVoucher_Resize(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            RemoveBlankLastRowFirst();
            if (btnAdd.Text == "Add")
            {
                txtDocNum.Text = GetNewSeries();

                JVUnit oUnit = new JVUnit();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.CardCode = txtCardCode.Text;
                oUnit.CardName = txtCardName.Text;
                oUnit.Remarks = txtRemarks.Text;

                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;
                oUnit.LineItems = this.LineItems;

                JVManager oManager = new JVManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.AddJournalVoucher(oUnit);

                oManager.Close();

                btnAdd.Text = "Update";
            }
            else
            {
                
                JVUnit oUnit = new JVUnit();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.CardCode = txtCardCode.Text;
                oUnit.CardName = txtCardName.Text;
                oUnit.Remarks = txtRemarks.Text;
                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;
                oUnit.LineItems = this.LineItems;

                JVManager oManager = new JVManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.UpdateJournalVoucher(oUnit);

                oManager.Close();

                btnAdd.Text = "Update";
            }

        }

        void RemoveBlankLastRowFirst()
        {
            int iCount = -1;
            gridView1.MoveLast();
            try
            {
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];
                string _ACCOUNT_CODE = "";


                _ACCOUNT_CODE = gridView1.GetRowCellValue(iCount, gridView1.Columns["AcctCode"]).ToString();

                if (_ACCOUNT_CODE.Trim() == "")
                {
                    gridView1.DeleteRow(iCount);
                }
            }
            catch { }

        }

        private void txtStatus_EditValueChanged(object sender, EventArgs e)
        {
            if (txtStatus.Text == "Draft")
            {
                txtStatus.Properties.Buttons[0].Visible = true;
                txtStatus.Properties.Buttons[1].Visible = true;
                EnableControls();

            }
            if (txtStatus.Text == "Canceled" || txtStatus.Text == "Closed")
            {
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
                DisableControls();

            }

           

            if (txtStatus.Text == "Posted")
            {
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
                DisableControls();

            }

            if (txtStatus.Text == "Open")
            {
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
                DisableControls();
                
            }
        }

        private void txtCardCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_BP _SearchBP = new FND_BP();
            _SearchBP.BusinessPartnerType = "Customer";
            _SearchBP.ConnectionString = this.ConnectionString;
            _SearchBP.ShowDialog();

            txtCardCode.Text = _SearchBP.CardCode;
            txtCardName.Text = _SearchBP.CardName;

        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Are you sure you want to delete this row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    gridView1.DeleteRow(gridView1.FocusedRowHandle);
                }
            }

            if (e.KeyCode == Keys.Down)
            {
                if (gridView1.IsLastRow)
                {

                    string sItemCode = "";

                    int iCount = gridView1.FocusedRowHandle;
                    sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["AcctCode"]).ToString();

                    if (sItemCode != "" && (txtStatus.Text == "Draft"))
                    {
                        gridView1.AddNewRow();

                        gridControl1.Select();
                        gridView1.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
                        gridView1.FocusedColumn = gridView1.Columns["AcctCode"];
                        gridView1.ShowEditor();


                    }
                }
            }
        }

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            switch (e.Button.Index)
            {
                case 0:
                    // Post

                    PostDocument();

                    break;
                case 1:
                    // Cancel:
                    if (txtStatus.Text == "Draft")
                    {
                        CancelDocument();
                    }
                    else if (txtStatus.Text == "Posted")
                    {
                        CloseDocument();
                    }
                    break;
            }
        }

        private void txtDebit_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Credit"], 0);
        }

        private void txtCredit_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Debit"], 0);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
